package com.study.demo01.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
//@Configurable
public class TtlQueueConfig {
    //绑定关系 如图 https://img-blog.csdnimg.cn/e18a79486c0b47d7b36aaafe9987b090.png
    //普通交换机
    public static final String X_EXCHANGE = "X";


    /**
     * 死信交换机
     */
    public static final String Y_DEAD_EXCHANGE = "Y";


    /**
     * 普通队列
     */
    public static final String QUEUE_A = "QA";
    public static final String QUEUE_B = "QB";

    //通用队列-可以作为延迟队列
    public static final String QUEUE_C = "QC";

    /**
     * 死信队列
     */
    public static final String DEAD_LETTER_QUEUE = "QD";


    //普通交换机
    @Bean("xExchange")
    public DirectExchange xExchange() {
        return ExchangeBuilder.directExchange(X_EXCHANGE).durable(true).build();
    }

    //死信交换机
    @Bean("yExchange")
    public DirectExchange Exchange() {
        return ExchangeBuilder.directExchange(Y_DEAD_EXCHANGE).durable(false).build();
    }

    //声明QUEUE_INFORM_EMAIL队列
    @Bean("queueA")
    public Queue queueA() {
        //死信交换机
        Map<String, Object> args = new HashMap<>(3);
        //消息过期后，进入到死信交换机
        args.put("x-dead-letter-exchange", Y_DEAD_EXCHANGE);
        //消息过期后，进入到死信交换机的路由key
        args.put("x-dead-letter-routing-key", "YD");
        //过期时间，单位毫秒
        args.put("x-message-ttl", 10000);
        return QueueBuilder.durable(QUEUE_A).withArguments(args).build();
    }

    //声明 队列
    @Bean("queueB")
    public Queue queueB() {
        Map<String, Object> args = new HashMap<>(3);
        args.put("x-dead-letter-exchange", Y_DEAD_EXCHANGE);
        args.put("x-dead-letter-routing-key", "YD");
        args.put("x-message-ttl", 40000);
        return QueueBuilder.durable(QUEUE_B).withArguments(args).build();
    }


    @Bean("queueC")
    public Queue queueC() {
        Map<String, Object> args = new HashMap<>(3);
        //设置死信交换机
        args.put("x-dead-letter-exchange", Y_DEAD_EXCHANGE);
        args.put("x-dead-letter-routing-key", "YD");
        return QueueBuilder.durable(QUEUE_C).withArguments(args).deadLetterRoutingKey("YD").build();
    }

    /**
     * 死信队列
     *
     * @return
     */
    @Bean("queueD")
    public Queue queueD() {
        return QueueBuilder.durable(DEAD_LETTER_QUEUE).build();
    }

    @Bean
    public Binding queueABindX(@Qualifier("queueA") Queue que, @Qualifier("xExchange") DirectExchange exchange) {
        return BindingBuilder.bind(que).to(exchange).with("XA");
    }

    @Bean
    public Binding queueBBindX(@Qualifier("queueB") Queue que, @Qualifier("xExchange") DirectExchange exchange) {
        return BindingBuilder.bind(que).to(exchange).with("XB");
    }

    @Bean
    public Binding queuedBindy(@Qualifier("queueD") Queue que, @Qualifier("yExchange") DirectExchange exchange) {
        return BindingBuilder.bind(que).to(exchange).with("YD");
    }

    @Bean
    public Binding queueCBindy(@Qualifier("queueC") Queue que, @Qualifier("xExchange") DirectExchange exchange) {
        return BindingBuilder.bind(que).to(exchange).with("XC");
    }
}
